package com.ssyc.student.adapter;

import android.content.Context;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.ssyc.common.adapter.CommonAdapter;
import com.ssyc.common.adapter.ViewHolder;
import com.ssyc.common.utils.ScreenUtils;
import com.ssyc.common.view.StatusBarView;
import com.ssyc.student.R;
import com.ssyc.student.bean.ChoosePetInfo;

import java.util.List;

/**
 * 展示宠物列表的适配器
 * Created by Administrator on 2018/4/18 0018.
 */

public class ChoosePetGvAdapter extends CommonAdapter<ChoosePetInfo> {

    private Context context;
    private RelativeLayout.LayoutParams lp;
    private List<ChoosePetInfo> mDatas;

    public ChoosePetGvAdapter(Context context, List<ChoosePetInfo> datas, int layoutId) {
        super(context, datas, layoutId);
        this.context = context;
        this.mDatas = datas;
        //计算图片的高度和宽度
        int lvWidth = (ScreenUtils.getScreenWidth(context) - 8 * 3) / 2;
        int lvHeight = (((ScreenUtils.getScreenHeight(context) - 100 - StatusBarView.getStatusBarHeight(context)) * 348 / (112 + 340 + 108)) - 24) / 2;
        lp = new RelativeLayout.LayoutParams(lvWidth, lvHeight * 82 / 100);

    }

    @Override
    public void convert(ViewHolder holder, ChoosePetInfo choosePetInfo) {
        ImageView lv = holder.getView(R.id.lv);
        lv.setLayoutParams(lp);
        if (choosePetInfo.isSelected) {
            lv.setImageResource(choosePetInfo.selectedDrawable);
        } else {
            lv.setImageResource(choosePetInfo.unSelectedDrawable);
        }
    }

    /**
     * 选中某个位置进行更新的方法
     *
     * @param position
     */
    public void updateState(int position) {
        if (null != mDatas) {
            for (int i = 0; i < mDatas.size(); i++) {
                if (position == i) {
                    mDatas.get(position).isSelected = true;
                } else {
                    mDatas.get(i).isSelected = false;
                }
            }
            notifyDataSetChanged();
        }
    }
}
